<template>
	<view class="search">
		<view class="query flex flex-jc-sb flex-ai-c">
			<view class="ml-20 mr-50">{{inits.addr.city}}</view>
			<view class="f1"
				style="border-left: 1rpx solid #D8D8D8;border-right: 1rpx solid #D8D8D8;padding: 0 30rpx;color: #999999;">
				<u-input placeholder="搜索城市/地点/基地" v-model="searchValue.text" />
			</view>
			<view class="ml-25" @click="chooseAddr">
				<i-icon icon="icon-zhongxindingwei" size="30rpx" />
			</view>
		</view>
		<view class="dete flex flex-jc-sb flex-ai-c" @click="jump(`/pages/common/calendarSelection`)">
			<view>
				<text class="mr-18 text">{{calendarData.start.month}}月{{calendarData.start.day}}日</text>
				{{calendarData.start.weekText}}
			</view>
			<view>
				<view class="line">
					<text style="color: #333333;">{{calendarData.diff}}</text>晚
				</view>
			</view>
			<view>
				<text class="mr-18 text">{{calendarData.end.month}}月{{calendarData.end.day}}日</text>
				{{calendarData.end.weekText}}
			</view>
		</view>
		<view class="btn" @click="search"><text>搜索基地</text></view>
	</view>
</template>

<script lang="ts" setup>
	import {
		reactive,onMounted
	} from 'vue'
	import { useStore } from 'vuex'
	import getLocation from '../utils/location.js';
	const store = useStore()
	const inits = reactive({
		addr: uni.getStorageSync('address'),
		date: {}
	})
	const searchValue = reactive({
		text: '',
		lon: 0.00,
		lng: 0.00,
	})
	const emit = defineEmits(['search','init'])
	interface Props {
		calendarData?: any
	}
	const props = withDefaults(defineProps<Props>(), {
		calendarData: {
			start: {
				month: '00',
				day: '00',
				weekText: ''
			},
			end: {
				month: '00',
				day: '00',
				weekText: ''
			},
			diff: 1
		}
	})
	const chooseAddr = () => {
		uni.showLoading({
			title: '加载中'
		})
		getLocation(true).then((res) => {
			inits.addr = res
			uni.hideLoading()
		}).catch((res) => {
			uni.hideLoading()
		})
	}
	const jump = (url : string) => {
		uni.navigateTo({
			url: url
		})
	}
	const location = (isModal = false) => {
		getLocation(isModal).then((res) => {
			inits.addr = res
			emit('init', res)
		}).catch((res) => {
			inits.addr = res
			emit('init', res)
		})
	}
	
	const search = () => {
		// if(!searchValue.text) return showToast('请输入搜索条件')
		uni.navigateTo({
			url: '/pages/hotel/list'
		})
		store.commit('CalendarData', {
			...props.calendarData,
			...searchValue,
			...inits.addr
		})
		emit('search', {
			...props.calendarData,
			...searchValue,
			...inits.addr
		})
	}
	onMounted(() => {
		location()
	})
</script>

<style lang="scss" scoped>
	.search {
		padding: 0 30rpx;
		height: 100%;
		background: #FFFFFF;
		border-radius: 20rpx;
		width: 100%;
		border: 1px solid #fff;
		.query {
			font-size: 30rpx;
			padding: 50rpx 0 32rpx 0;
		}

		.dete {
			color: #999999;
			font-size: 22rpx;
			border-top: 1rpx solid #D8D8D8;
			border-bottom: 1rpx solid #D8D8D8;
			padding: 39rpx 0;

			.text {
				font-size: 32rpx;
				color: #333333;
			}
		}

		.btn {
			height: 94rpx;
			background: #009943;
			border-radius: 9rpx;
			margin: 66rpx 0 46rpx 0;
			line-height: 94rpx;
			text-align: center;
			font-size: 32rpx;

			text {
				color: #fff;
			}
		}
	}
</style>